package cn.tqfeiyang.smartadmin.system.controller;

import cn.tqfeiyang.smartadmin.system.service.ConfigService;
import cn.tqfeiyang.smartadmin.system.request.ConfigCreateDTO;
import cn.tqfeiyang.smartadmin.system.request.ConfigUpdateDTO;
import cn.tqfeiyang.smartadmin.system.request.ConfigQueryDTO;
import cn.tqfeiyang.smartadmin.system.response.ConfigQueryVO;
import cn.tqfeiyang.smartadmin.system.response.ConfigDetailVO;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import cn.dev33.satoken.annotation.SaCheckPermission;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import cn.tqfeiyang.smartadmin.commons.pagination.PageData;
import cn.tqfeiyang.smartadmin.commons.response.ApiResult;
import cn.tqfeiyang.smartadmin.commons.validation.EntityId;
import cn.tqfeiyang.smartadmin.commons.logging.OperationLog;

/**
 * 配置控制器
 *
 * @author tqfeiyang
 * @since 2024-08-23
 */
@RestController
@Validated
@RequestMapping("/api/system/config")
@Tag(name = "系统/配置")
public class ConfigController {

    /**
     * 配置服务
     */
    @Resource
    private ConfigService configService;

    /**
     * 查询配置
     *
     * @param dto 请求数据
     * @return 配置列表
     */
    @OperationLog
    @GetMapping("/query")
    @Operation(summary = "查询配置")
    @SaCheckPermission("system:config:query")
    public ApiResult<PageData<ConfigQueryVO>> queryConfigs(@Validated ConfigQueryDTO dto) {
        return ApiResult.success(configService.queryConfigs(dto));
    }

    /**
     * 获取配置
     *
     * @param configId 配置id
     * @return 配置信息
     */
    @OperationLog
    @GetMapping("/{configId}/read")
    @Operation(summary = "获取配置")
    @SaCheckPermission("system:config:read")
    public ApiResult<ConfigDetailVO> readConfig(@PathVariable @EntityId Long configId) {
        return ApiResult.success(configService.readConfig(configId));
    }

    /**
     * 添加配置
     *
     * @param dto 请求数据
     * @return 配置信息
     */
    @OperationLog
    @PostMapping("/create")
    @Operation(summary = "添加配置")
    @SaCheckPermission("system:config:create")
    public ApiResult<ConfigDetailVO> createConfig(@RequestBody @Validated ConfigCreateDTO dto) {
        Long configId = configService.createConfig(dto);
        return ApiResult.success(configService.readConfig(configId));
    }

    /**
     * 修改配置
     *
     * @param configId 配置id
     * @param dto 请求数据
     * @return 配置信息
     */
    @OperationLog
    @PostMapping("/{configId}/update")
    @Operation(summary = "修改配置")
    @SaCheckPermission("system:config:update")
    public ApiResult<ConfigDetailVO> updateConfig(@PathVariable @EntityId Long configId, @RequestBody @Validated ConfigUpdateDTO dto) {
        configService.updateConfig(configId, dto);
        return ApiResult.success(configService.readConfig(configId));
    }

    /**
     * 删除配置
     *
     * @param configId 配置id
     */
    @OperationLog
    @PostMapping("/{configId}/delete")
    @Operation(summary = "删除配置")
    @SaCheckPermission("system:config:delete")
    public ApiResult<String> deleteConfig(@PathVariable @EntityId Long configId) {
        configService.deleteConfig(configId);
        return ApiResult.success();
    }
}
